package com.qujia.integral.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.qujia.common.core.domain.UniversalRedisMessageDto;
import com.qujia.common.enums.WxConsumptionMessageEnum;
import com.qujia.common.utils.DateUtils;
import com.qujia.enums.RecordTypeEnum;
import com.qujia.enums.integralTypeEnum;
import com.qujia.integral.domain.CustomerPointsRecord;
import com.qujia.integral.domain.dto.CustomerPointsRecordResDto;
import com.qujia.integral.domain.vo.CustomerPointsRecordReqVo;
import com.qujia.integral.domain.vo.CustomerPointsRecordResVo;
import com.qujia.integral.mapper.CustomerPointsRecordMapper;
import com.qujia.integral.service.ICustomerPointsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户积分记录Service业务层处理
 * 
 * @author qujia
 * @date 2021-03-17
 */
@Service
public class CustomerPointsRecordServiceImpl implements ICustomerPointsRecordService 
{
    @Autowired
    private CustomerPointsRecordMapper customerPointsRecordMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${message.channel}")
    private String channel;
    /**
     * 查询客户积分记录
     * 
     * @param id 客户积分记录ID
     * @return 客户积分记录
     */
    @Override
    public CustomerPointsRecord selectCustomerPointsRecordById(Long id)
    {
        return customerPointsRecordMapper.selectCustomerPointsRecordById(id);
    }

    /**
     * 查询客户积分记录列表
     * 
     * @param customerPointsRecord 客户积分记录
     * @return 客户积分记录
     */
    @Override
    public List<CustomerPointsRecord> selectCustomerPointsRecordList(CustomerPointsRecord customerPointsRecord)
    {
        return customerPointsRecordMapper.selectCustomerPointsRecordList(customerPointsRecord);
    }

    /**
     * 新增客户积分记录
     * 
     * @param customerPointsRecord 客户积分记录
     * @return 结果
     */
    @Override
    public int insertCustomerPointsRecord(CustomerPointsRecord customerPointsRecord)
    {
        customerPointsRecord.setCreateTime(DateUtils.getNowDate());
        customerPointsRecord.setAction(integralTypeEnum.MANUAL_ADDITION);

        return customerPointsRecordMapper.insertCustomerPointsRecord(customerPointsRecord);
    }

    /**
     * 修改客户积分记录
     * 
     * @param customerPointsRecord 客户积分记录
     * @return 结果
     */
    @Override
    public int updateCustomerPointsRecord(CustomerPointsRecord customerPointsRecord)
    {
        return customerPointsRecordMapper.updateCustomerPointsRecord(customerPointsRecord);
    }

    /**
     * 批量删除客户积分记录
     * 
     * @param ids 需要删除的客户积分记录ID
     * @return 结果
     */
    @Override
    public int deleteCustomerPointsRecordByIds(Long[] ids)
    {
        return customerPointsRecordMapper.deleteCustomerPointsRecordByIds(ids);
    }

    /**
     * 删除客户积分记录信息
     * 
     * @param id 客户积分记录ID
     * @return 结果
     */
    @Override
    public int deleteCustomerPointsRecordById(Long id)
    {
        return customerPointsRecordMapper.deleteCustomerPointsRecordById(id);
    }

    @Override
    public List<CustomerPointsRecord> selectList(CustomerPointsRecord record){
        return customerPointsRecordMapper.selectList(record);
    }

    @Override
    public CustomerPointsRecord getOne(CustomerPointsRecord record){
        return customerPointsRecordMapper.getOne(record);
    }

    @Override
    public void wxgzhIntegralPush(CustomerPointsRecord record) {
        UniversalRedisMessageDto dto=new UniversalRedisMessageDto();
        dto.setData(JSONObject.toJSONString(record));
        dto.setMessageType(WxConsumptionMessageEnum.INTEGRAL_MESSAGE_PUSH.getValue());
        dto.setTitle(WxConsumptionMessageEnum.INTEGRAL_MESSAGE_PUSH.getKey());
        redisTemplate.convertAndSend(channel, dto);
    }

    @Override
    public CustomerPointsRecordResVo selectCustomerPointsRecordDetailList(CustomerPointsRecordReqVo customerPointsRecord) {
        CustomerPointsRecordResVo resVo = new CustomerPointsRecordResVo();
        List<CustomerPointsRecordResDto> resDtos = customerPointsRecordMapper.selectCustomerPointsRecordDetailList(customerPointsRecord);
        PageHelper.clearPage();
        List<CustomerPointsRecordResDto> resCount = customerPointsRecordMapper.countCustomerPointsRecordDetailList(customerPointsRecord);
        long increase = 0;
        long decrease = 0;
        if (null != resCount && resCount.size() > 0) {
            for (CustomerPointsRecordResDto data : resCount) {
                if (RecordTypeEnum.INCREASE.getName().equals(data.getRecordType())) {
                    increase = data.getIntegral();
                }
                if (RecordTypeEnum.DECREASE.getName().equals(data.getRecordType())) {
                    decrease = data.getIntegral();
                }
            }
        }
        resVo.setIntegralData(resDtos);
        resVo.setIntegralDecrease(decrease);
        resVo.setIntegralIncrease(increase);
        resVo.setIntegralTotal(increase - decrease);
        return resVo;
    }


}
